import java.io.*;

public class FindAndReplace {
	public static void main(String[] args) throws IOException {
		if (args.length < 3) {
			throw new RuntimeException("expected: filePath fromRegExp toRegexp");
		}
//		System.out.println(args[0] + " " + args[1] + " " + args[2]);
		for (int i = 0; i + 2 < args.length; i += 3) {
			findAndRep(args[i], args[i + 1], args[i + 2]);
		}
	}

	private static void findAndRep(String file, String from, String to)
		throws FileNotFoundException, IOException {
		InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
		StringBuilder sb = new StringBuilder();
		char[] buff = new char[1000];
		while (true) {
			int l = isr.read(buff);
			if (l <= 0) {
				break;
			}
			sb.append(buff, 0, l);
		}
		isr.close();
		String result = sb.toString().replaceAll(from, to);
		PrintWriter pw = new PrintWriter(file);
		pw.print(result);
		pw.close();
	}
}